exports.discountsDetail = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var activityBrowsing = 0;

    var browseTimes = 0;
    //任务流创建
    async.waterfall([

        

        //获取活动详细
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganizationActivity + "/oa_id/" + restfulParams.oa_id,
                    method: 'GET',
                },
                function (err, data) {
                    
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    if(data.data.length > 0){

                        resultData.activityData = data.data[0];
                        resultData.joinUserCount = data.data[0].oa_join_count;
                    }
                   
                    cb();
                   
                }
            );

        },
        function (cb){
        var activityData = resultData.activityData;
        if(!activityData){
            resultData.activityData = [];
            cb();
        }else{
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.updateOrganizationActivity,
                    method: 'POST',
                    data:{
                        oa_id:resultData.activityData.oa_id,
                        oa_browse_count:parseInt(parseInt(resultData.activityData.oa_browse_count) + 1),
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    cb();

                }
            );
        }

        },
        
        //获取扩展表信息
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.discountsActivitySearch + "/dac_oaid/" + restfulParams.oa_id,
                    method: 'POST',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    if(data.data.length>0){
                        resultData.configData = data.data[0];
                    }else{
                        resultData.configData = [];
                    }
                    //console.log(resultData.configData)

                    cb();
                    
                }
            );

        },
        //获取优惠活动内容
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_discounts_activity_content + "/dac_oaid/" + restfulParams.oa_id,
                    method: 'POST',
                    data: {
                        fields: ["dac_content","dac_type"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    if(resultData.activityData.oa_role != null && data.data.length == 0){
                        resultData.contentData = [];
                        resultData.contentData[0] = 3;
                    }else{
                        resultData.contentData = data.data;
                    }
                    cb();

                }
            );

        },
        //获取机构信息
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + resultData.activityData.oa_oid,
                    method: 'GET',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    resultData.organizationData = data.data[0];

                    cb();
                    
                }
            );

        },

        //查询已报名人(页面显示)
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchDiscountsActivityUserSearch + "/count/1/dau_oaid/" + restfulParams.oa_id+"/limit/7/sort_order/dau_create_time-asc",
                    method: 'GET',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    // resultData.joinUserCount = data.count;
                    resultData.joinUserData  = data.data;

                    cb();
                    
                }
            );

        },
        //查询已报名人(页面显示)
        // function(cb){
        //
        //     R.SEND_HTTP(
        //         req,
        //         {
        //             url: CORE_APIConfig.coreAPI.searchDiscountsActivityUserSearch + "/count/1/dau_oaid/" + restfulParams.oa_id,
        //             method: 'POST',
        //             data:{
        //                 fields:['dau_id'].join(",")
        //             }
        //         },
        //         function (err, data) {
        //             //判断是否有错误信息
        //             if (err) {
        //                 return cb(err, data);
        //             }
        //
        //             resultData.joinUserCount = data.count;
        //             // resultData.joinUserData  = data.data;
        //
        //             cb();
        //
        //         }
        //     );
        //
        // },

        //查询评论信息
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.activity_comment_info + "/oac_oaid/" + restfulParams.oa_id + "/skip/0/limit/20/count/1",
                    method: 'GET',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.commentCount = data.count;
                    resultData.commentData  = data.data;

                    cb();
                    
                }
            );
        },

        //查询是否收藏过该活动
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/1/uc_fid/" + restfulParams.oa_id + "/uc_uid/" + restfulParams.u_id,
                    method: 'GET',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    //console.log(data);
                    if(data.data.length > 0){
                        resultData.isCollect = true;
                    }else{
                        resultData.isCollect = false;
                    }

                    cb();
                    
                }
            );

        },

        //查询收藏人数
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/1/uc_fid/" + restfulParams.oa_id + "/count/1",
                    method: 'GET',
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.collectCount = data.count;

                    cb();
                    
                }
            );
        },

        /////
        //查询用户是否浏览过
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivityBrowsingHistory + "/ubh_oaid/" + restfulParams.oa_id + "/ubh_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['ubh_id'].join(",")
                        }
                    },
                    function (err, data) {
                        activityBrowsing = data.data;
                        cb();
                    }
                );
            } else {
                cb();
            }
        },
        //查询浏览次数
        function (cb) {
            if (restfulParams.u_id) {
                if (activityBrowsing.length > 0) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchOrganizationActivityBrowsingHistory + "/ubh_oaid/" + restfulParams.oa_id + "/ubh_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ['ubh_id','ubh_count'].join(",")
                            }
                        },
                        function (err, data) {

                            browseTimes = data.data;

                            cb();
                        }
                    );
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.addOrganizationActivityBrowsingHistory,
                            method: 'POST',
                            data: {
                                ubh_oaid: restfulParams.oa_id,
                                ubh_uid: restfulParams.u_id
                            }
                        },
                        function (err, data) {

                            resultData.addData = data.data;

                            cb();
                        }
                    );
                }
            } else {
                cb();
            }

        },
        //更新浏览次数与浏览时间
        function (cb) {
            if (restfulParams.u_id) {
                if (activityBrowsing.length > 0) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganizationActivityBrowsingHistory,
                            method: 'POST',
                            data: {
                                ubh_id: activityBrowsing[0].ubh_id,
                                ubh_create_time: Date.parse(new Date()) * 0.001,
                                ubh_count: parseInt(browseTimes[0].ubh_count) + 1
                            }
                        },
                        function (err, data) {

                            browseTimes = data.data;

                            cb();
                        }
                    );
                } else {
                    cb();
                }
            } else {
                cb();
            }
        },


        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );

};